/**
 * eCommerce Application Sample for J2EE Training 
 * Implementation of EcomAdminBean
 * @author Fabienne Boyer - Didier Donsez
 * may 2006
 */

package ecom.beans;

import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import javax.ejb.Remote;
import javax.ejb.SessionSynchronization;
import javax.ejb.Stateful;
import javax.persistence.CascadeType;
import javax.persistence.EntityManager;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import ecom.pojos.TechQuestion;

@Stateful
@Remote(EcomAdminRemote.class)
public class EcomAdminBean implements EcomAdminRemote {

	@PersistenceContext
	private EntityManager entityManager = null;

	/*public void createAccount(int accountId, int accountOwner, double balance) {
		
		AccountBean ab = new AccountBean(accountId, accountOwner, balance);
		entityManager.persist(ab);
		
		//if the owner isn't the admin, we need to update the owner's customerbean
		if (accountOwner != 0) setCustomerAccount(ab);
		
	}*/
	
	public void createAccount(String emailId, double balance) {
		
		AccountBean ab = new AccountBean(emailId, balance);
		entityManager.persist(ab);
		
		//if the owner isn't the admin, we need to update the owner's customerbean
		if (emailId != null) setCustomerAccount(ab);
		
	}

	
	public AccountBean getAccount(String email) {
		AccountBean ab;
		Query q = entityManager.createQuery("SELECT a FROM AccountBean a WHERE a.emailId = :email");
		ab = (AccountBean) q.setParameter("email", email).getSingleResult();
		//ab = entityManager.find(AccountBean.class, accountId);
		return ab;
	}
	

	public void createProduct(String reference, String productName, String productBrand,
			double productPrice, String productCat, String productDescr, double productStock) {
		ProductBean p = new ProductBean(reference,productCat,productName,productBrand, productDescr,productPrice,productStock);
		entityManager.persist(p);
	}
	
	public ProductBean getProduct(String prodId) {
		ProductBean pb = entityManager.find(ProductBean.class, prodId);
		return pb;
	}
	
	public List<AccountBean> getAllAccounts() {
		Query accounts = entityManager.createNamedQuery("AllAccounts");
		return accounts.getResultList();
	}
	
	public List<CustomerBean> getAllCustomers() {
		Query customers = entityManager.createNamedQuery("AllCustomers");
		return customers.getResultList();
	}
	
	public Vector<String> getAllAccountsName() {
		Vector<String> res = new Vector<String>();
		List<AccountBean> accounts = getAllAccounts();
		Iterator ite = accounts.iterator();
		
		while(ite.hasNext())
		{
			res.add(ite.next().toString());
		}
		
		return res;
	}
	
	public Vector<String> getAllCustomersName() {
		Vector<String> res = new Vector<String>();
		List<CustomerBean> customers = getAllCustomers();
		Iterator<CustomerBean> ite = customers.iterator();
		
		while(ite.hasNext())
		{
			CustomerBean cur = ite.next(); 
			res.add(cur.toString());
		}
		
		return res;
	}
	
	public String getAccountName(String email) {
		return getAccount(email).toString();
	}
	
	


	public String getProductName(String prodId) {
		return getProduct(prodId).name;
	}

	public List<ProductBean> getAllProducts() {
		Query prods = entityManager.createQuery("select p from ProductBean p");
		return prods.getResultList();
	}
	
	public Vector<String> getAllProductsName() {
		Vector<String> res = new Vector<String>();
		List<ProductBean> prods = getAllProducts();
		Iterator ite = prods.iterator();
		
		while(ite.hasNext())
		{
			res.add(ite.next().toString());
		}
		
		return res;
	}
	
	/*public void createCustomer(int customerId, int accountId, byte[] pwd, String nom,
			String prenom, String adresse, int codePostal, String country,
			int tel, String email) {
		
		CustomerBean custBean = new CustomerBean(customerId,accountId,pwd,nom,prenom,adresse,codePostal,country,tel,email);
		entityManager.persist(custBean);
	}*/


	public void createCustomer(byte[] pwd, String nom,
			String prenom, String adresse, int codePostal, String country,
			String tel, String email) {
		
		CustomerBean custBean = new CustomerBean(pwd,nom,prenom,adresse,codePostal,country,tel,email);
		entityManager.persist(custBean);
	}	
	
	public void createCategory(int categoryId, String name) {
		
		CategoryBean categBean = new CategoryBean(categoryId,name);
		entityManager.persist(categBean);
	}
	

	/*private void setCustomerAccount(AccountBean a) {
		CustomerBean cb = entityManager.find(CustomerBean.class, a.accountOwnerId);
		if (cb != null) cb.setAccount(a);
	}*/
	private void setCustomerAccount(AccountBean a) {
		CustomerBean cb = entityManager.find(CustomerBean.class, a.emailId);
		if (cb != null) cb.setAccount(a);
	}


	public Vector<TechQuestion> getUnansweredTechQuestion() {
		Vector<TechQuestion> res = new Vector<TechQuestion>();
		Query q = entityManager.createQuery("SELECT tq FROM TechQuestionBean tq WHERE tq.reponse = null");
		List<TechQuestionBean> tq = q.getResultList();
		Iterator<TechQuestionBean> ite = tq.iterator();
		
		while(ite.hasNext()) {
			TechQuestionBean cur = ite.next();
			res.add(new TechQuestion(cur.getQuestion(), cur.getReponse(),cur.getId(),cur.getProductId()));
		}
		
		return res;
	}


	public void answerTechQuestion(int idQ, String answer) {
		Query q = entityManager.createQuery("SELECT tq FROM TechQuestionBean tq WHERE tq.id = :idQ");
		TechQuestionBean tq = (TechQuestionBean) q.setParameter("idQ", idQ).getSingleResult();
		tq.setReponse(answer);
	}
}
